package com.yundingai.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yundingai.domain.entity.AllUser;
import com.yundingai.domain.vo.LoginVo;
import org.apache.ibatis.annotations.*;

/**
* @author liyuxiang
* @description 针对表【all_user(用户信息表：记录用户信息)】的数据库操作Mapper
* @createDate 2025-07-16 12:50:48
* @Entity com.yundingai.domain.entity.AllUser
*/
@Mapper
public interface AllUserMapper extends BaseMapper<AllUser> {

    //保存用户头像
    @Update("update all_user set avatar_url = #{avatarUrl} where user_id = #{userId}")
    void saveAvatarUrl(Integer userId,String avatarUrl);

    //根据用户名和密码查询用户
    @Select("SELECT u.user_id,u.username,u.nickname,u.gender,u.avatar_url,u.user_phone,u.email,u.study_time,u.STATUS,r.role_id,r.role_name,r.role_desc FROM all_user u LEFT JOIN rel_user_role ur ON u.user_id = ur.user_id LEFT JOIN all_role r ON ur.role_id = r.role_id WHERE u.username = #{username} AND u.`password` = #{password}")
    LoginVo login(String username, String password);

    //用户注册的方法
    @Insert("insert into all_user (username,password,email) values (#{username},#{password},#{email})")
    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
    void register(AllUser allUser);


    @Select("select r.role_id from all_role r join rel_user_role ru on r.role_id=ru.role_id join all_user u on u.user_id=ru.user_id where u.username=#{username}")
    Integer findUserRole(String username);

    @Select("select r.role_id from all_role r join rel_user_role ru on r.role_id=ru.role_id where ru.user_id=#{userId}")
    Integer findUserRoleByUserId(Integer userId);

    //修改用户头像
    @Update("update all_user set avatar_url=#{avatarUrl} where user_id=#{userId}")
    void updateAvatarUrl(@Param("userId") Integer userId, @Param("avatarUrl") String avatarUrl);

    /**
     * 查询用户总数
     * @return 用户总数
     */
    @Select("SELECT COUNT(*) FROM all_user WHERE status = 1") // 只统计状态正常的用户
    Integer selectTotalUserCount();

    //多条件修改用户基础信息
    void updateUserInfo(AllUser allUser);

    //修改密码的方法
    @Update("update all_user set password=#{password} where user_id=#{userId}")
    void updatePassword(@Param("userId") Integer userId, @Param("password") String password);
}




